import Koa from 'koa'
import cors from '@koa/cors'
import IP from 'ip'
import bodyParser from 'koa-bodyparser'
import loggerMiddleware from './middleware/logger'

// 路径别名 '@' => 'src'
import topicsRouter from '@/routes/topics'
import config from './config'

const app = new Koa()

// 日志中间件需要放在做前面,否则后续的请求无法监听
app.use(loggerMiddleware)
app.use(cors())
app.use(bodyParser())

// 路由
app.use(topicsRouter.routes())
app.use(topicsRouter.allowedMethods())

app.listen(config.server.port, () => {
  const local = IP.address()
  console.log(`Koa server listening at: http://${local}:${config.server.port}`)
})
